<?php
	include_once "config_db.php";
	$database = 'mymap';
	
	if (!mysql_connect($db_host, $db_user, $db_pwd))
	die("Can't connect to database");
		
	if (!mysql_select_db($database))
	die("Can't select database");
		
	mysql_query("SET NAMES 'utf8'");
	
	$i = 0;
	$j = 0;
	
	// load SimpleXML
	function loadXML() {
		$name_document = "xml/map.xml";
		$xml = simplexml_load_file($name_document);
		return $xml;
	}
	
	function loadXML_Way() {
		$name_document = "xml/way.xml";
		$xml = simplexml_load_file($name_document);
		return $xml;
	}
	//$nodes = $xml->node;
	//$ways = $xml->way;
	//getNodes($nodes);
//	getWays($ways);
//	$file_name = $_REQUEST['path_and_file_name']; 
	function getWays($ways) {
		foreach ($ways as $way) {
		    $id = $way->attributes()->id;		
		    $nds = $way->nd;
			$list_node = '';
			foreach ($nds as $nd) {
				$temp = $nd->attributes()->ref.", ";
				$list_node = $list_node.$temp;
				
			}	
			
		    $user = $way->attributes()->user;
		    
		    $version = $way->attributes()->version;
			$j++;
			if($j == 100){
				break;
			}
		}
	}	
	function getAllWays() {
		$xml = loadXML();
		$ways = $xml->way;
		$hasName = false;
		$i = 0;
		$j = 0;
		foreach ($ways as $way) {
		    $id = $way->attributes()->id;	
		    $tags = $way->tag;
			foreach ($tags as $tag) {
				$k = (string)$tag->attributes()->k;
				$v = (string)$tag->attributes()->v;
				if($k == "name") {
					echo "<option value=".$id.">".$v."</option>";
					$hasName = true;
					$j++;
					break;
				} else {
					$hasName = false;
				}
			}	
			if($hasName == false) {
				echo "<option value=".$id.">".$id."</option>";
			}
			$i++;
		}
		echo "<option value=".$j.">".$j."</option>";
	}
	function getWay($way_id, $isRequest) {
		$xml = loadXML();
		$ways = $xml->way;
		$i = 0;
		$node_array = array();
		foreach ($ways as $way) {
		    $id = $way->attributes()->id;	
			if($id == $way_id) {
				$nds = $way->nd;
				foreach ($nds as $nd) {
					$ref = $nd->attributes()->ref;
					$node_array[] = getNodes((string)$ref);
				}	
			}
		}
		if($isRequest) {
			echo '{"way":'.json_encode($node_array).'}';
		} else {
			return '{"way":'.json_encode($node_array).'}';;
		}
	}
	
	function getNodes($node_id) {
		$xml = loadXML();
		$nodes = $xml->node;
		foreach ($nodes as $node) {
		    $id = $node->attributes()->id;
			if((string)$id == $node_id) {
				$custom_node['id'] = $node_id;
				$custom_node['lat'] = (string)$node->attributes()->lat;
		    	$custom_node['lng'] = (string)$node->attributes()->lon;
				break;
			}		   		    
		    /*$result = insert_point($id, round_f($lat, 6), round_f($lng, 6), $version);
		    if($result){}
		    else {
		    	$stt = false;
		    	break;
		    }*/
		}
		return $custom_node;
		/*if($stt) {
			echo 'Finish!';	
		} else {
			echo 'Error!';
		}*/
	}	
	
	function getAllRelations() {
		$xml = loadXML();
		$relations = $xml->relation;
		$i = 0;
		$hasName = false;
		foreach ($relations as $relation) {
		    $id = $relation->attributes()->id;	
		    $tags = $relation->tag;
			foreach ($tags as $tag) {
				$k = (string)$tag->attributes()->k;
				$v = (string)$tag->attributes()->v;
				if($k == "name") {
					echo "<option value=".$id.">".$v."</option>";
					$hasName = true;
					break;
				} else {
					$hasName = false;
				}
			}	
			if($hasName == false) {
				echo "<option value=".$id.">".$id."</option>";
			}
			$i++;
		}
	}
	
	function getRelation($relation_id, $isRequest) {
		set_time_limit(3600);
		$xml = loadXML();
		$relations = $xml->relation;
		$array_way = array();
		foreach ($relations as $relation) {
		    $id = $relation->attributes()->id;	
			if($id = $relation_id) {
				$members = $relation->member;
				foreach ($members as $member) {
					$type = (string)$member->attributes()->type;
					$ref = (string)$member->attributes()->ref;
					if($type == "way") {
						$way = getWay($ref, false);
						$array_way[] = $way;
					}
				}
			}
		}
		if($isRequest) {
			echo '{"relations":'.json_encode($array_way).'}'; 
		} else {
			return '{"relations":'.json_encode($array_way).'}'; 
		}
	}
	
	function insert_AllNode() {
		$xml = loadXML();
		$nodes = $xml->node;
		$stt_node = true;
		$stt_tag = true;
		$result_tag = 1;
		foreach ($nodes as $node) {
		    $id = $node->attributes()->id;			
			$lat = $node->attributes()->lat;
			$lng = $node->attributes()->lon;		   		    
			$tags = $node->tag;
			if($tags != null) {
				foreach ($tags as $tag) {
					$k = $tag->attributes()->k;
					$v = $tag->attributes()->v;
					$v = str_replace( array( "'", '"', ',' , ';', '<', '>' ), '', $v);
					$result_tag = insert_node_tag($id, $k, $v);
				}
			} else {
				$result_tag = 1;
			}
		    $result_node = insert_node($id, round_f($lat, 6), round_f($lng, 6));
			if($result_node) {
				$stt_node = true;
			} else {
				$stt_node = false;
				break;
			}
		    if($result_tag){
				$stt_tag = true;
			}
		    else {
		    	$stt_tag = false;
		    	break;
		    }
		}
		if($stt_node && $result_tag) {
			echo 'Finish!';	
		} else {
			if($stt_node == false) {
				echo 'Error_Import_Node!';
			} elseif($stt_tag == false) {
				echo 'Error_Import_Tag!';
			} else {
				echo 'Error_Both!';
			}
		}
	}
	
	function insert_AllSegment() {
		$xml = loadXML();
		$ways = $xml->way;
		$start_node = null;
		$end_node = null;
		$result_tag = 1;
		$result_segment = 1;
		$result_segment_nd = 1;
		$stt_segment = true;
		$stt_segment_nd = true;
		$stt_tag = true;
		foreach ($ways as $way) {	
			$i = 0;
			$tag_array = null;
			$id = $way->attributes()->id;	
		    $tags = $way->tag;
			$nds = $way->nd;
			$isHighWay = false;
			
			if($tags != null) {
				foreach ($tags as $tag) {
					$k = (string)$tag->attributes()->k;				
					if($k == "highway") {					
						$isHighWay = true;
						break;
					} else {
						$isHighWay = false;
					}
				}	
				
				if($isHighWay) {
					foreach ($nds as $nd) {
						if($i == 0) {
							$start_node = $nd->attributes()->ref;
						}
						$end_node = $nd->attributes()->ref;
						$result_segment_nd = insert_segment_nd($id, $end_node);
						$i++;
					}
					foreach ($tags as $tag) {
						$k = (string)$tag->attributes()->k;			
						$v = (string)$tag->attributes()->v;
						$v = str_replace( array( "'", '"', ',' , ';', '<', '>' ), '', $v);	
						$result_tag = insert_segment_tag($id, $k, $v);
					}
					$result_segment = insert_segment($id, $start_node, $end_node);
				}
			}			
			if($result_segment) {
				$stt_segment = true;
			} else {
				$stt_segment = false;
				break;
			}
			if($result_segment_nd) {
				$stt_segment_nd = true;
			} else {
				$stt_segment_nd = false;
				break;
			}
		    if($result_tag){
				$stt_tag = true;
			}
		    else {
		    	$stt_tag = false;
		    	break;
		    }
		}
		if($stt_segment && $stt_segment_nd && $stt_tag) {
			echo 'Finish!';	
		} else {
			if($stt_segment == false) {
				echo 'Error_Import_Segment!';
			} elseif($stt_segment_nd == false) {
				echo 'Error_Import_Segment_ND!';
			} elseif($stt_tag == false) {
				echo 'Error_Import_Tag!';
			} else {
				echo 'Error_All!';
			}
		}
	}
	
	function insert_node($id, $lat, $lng) {
		set_time_limit(3600);
		$sql_query = "INSERT INTO node (id, lat, lng) 
					  VALUES (".$id.", ".$lat.", ".$lng.")";
		//			  echo $sql_query;
		$result = mysql_query($sql_query);
		return $result;
	}
	function insert_node_tag($node_id, $k, $v) {
		set_time_limit(3600);
		$sql_query = "INSERT INTO node_tag (node_id, k, value) 
					  VALUES (".$node_id.", '".$k."', '".$v."')";
		$result = mysql_query($sql_query);
		return $result;
	}
	function insert_segment($id, $start_node, $end_node) {
		set_time_limit(3600);
		$sql_query = "INSERT INTO segment (id, start_node, end_node) 
					  VALUES (".$id.", ".$start_node.", ".$end_node.")";
		//			  echo $sql_query;
		$result = mysql_query($sql_query);
		return $result;
	}
	function insert_segment_tag($segment_id, $k, $v) {
		set_time_limit(3600);
		$sql_query = "INSERT INTO segment_tag (segment_id, k, value) 
					  VALUES (".$segment_id.", '".$k."', '".$v."')";
		$result = mysql_query($sql_query);
		return $result;
	}
	
	function insert_segment_nd($segment_id, $nd) {
		set_time_limit(3600);
		$sql_query = "INSERT INTO segment_nd (segment_id, nd) 
					  VALUES (".$segment_id.", ".$nd.")";
		$result = mysql_query($sql_query);
		return $result;
	}
	function round_f($num, $ext){
		$tmp = (float) pow(10, $ext);
		$tmp_1 = (float) round((((float)$num)*$tmp));
		return (float)($tmp_1/$tmp);
	}
?>